﻿using System;
using System.Collections.Generic;
using System.Text;
using OneMG.Phoenix.Kernel.Util;
using System.Web;
using OneMG.Phoenix.Service.Model;
using OneMG.Phoenix.Service.Contracts;
using OneMG.Phoenix.Service;

namespace OneMG.Phoenix.AdminSite
{
    public class PageBase : System.Web.UI.Page
    {
        IAdminUserService IAdminUser = new AdminUserService();

        public void CheckLogin(object sender, System.EventArgs e)
        {
            string token = GetCookie("Token");

            if (string.IsNullOrEmpty(token))
            {
                Response.Redirect("http//xxx.com?done=" + Server.UrlEncode("http://" + Request.Url.Host + Request.Url.PathAndQuery));
            }
            int adminID = int.Parse(Decrypt(token));
            if (Session["AdminUser"] == null)
            {
                AdminUserModel model = IAdminUser.GetByUserID(adminID);
                Session["AdminUser"] = model; 
            }
           
        }

        private string GetCookie(string Key)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[Key];
            if (cookie != null)
            {
                try
                {
                    return cookie.Value;
                }
                catch
                {
                    return string.Empty;
                }
            }
            else
                return string.Empty;
        }


        #region 加密参数
        #region 属性
        private static string _Key = "EaSuNy#";
        private static string _IV = "eAsUnY3";
        protected static string EncryptKey
        {
            get
            {
                return _Key;
            }
        }

        protected static string EncryptIV
        {
            get
            {
                return _IV;
            }
        }
        #endregion


        private static string Encrypt(string strSource)
        {
            Alogrim objAlogrim = new Alogrim(Alogrim.SymmProvEnum.DES);
            objAlogrim.Key = EncryptKey;
            objAlogrim.IV = EncryptIV;
            return objAlogrim.Encrypting(strSource);
        }

        /// <summary>
        /// 解密字符串
        /// </summary>
        /// <param name="strSource">要解密的字符串</param>
        /// <returns>解密后的字符串</returns>
        private string Decrypt(string strSource)
        {
            Alogrim objAlogrim = new Alogrim(Alogrim.SymmProvEnum.DES);
            objAlogrim.Key = EncryptKey;
            objAlogrim.IV = EncryptIV;
            return objAlogrim.Decrypting(strSource);
        }
        #endregion
    }
}
